Skip to main content

Program Structure

Every Go program, no matter how big or small, follows a few key rules:

  1. Package Declaration – Tells Go what “package” this file belongs to.

    • If the program is meant to run directly, the package must be main.
  2. Import Statements – Brings in other code (libraries) you want to use.

  3. Functions – Blocks of code that do things.

    • The main function is special: it’s where your program starts running.
  4. Statements & Expressions – The actual instructions you give the computer

General Layout:

package main          // 1. Package declaration

import "fmt" // 2. Import statement

// 3. Function definition
func main() {
fmt.Println("Hello, World!") // 4. Statements
}

Key Points to Remember

  • Go is case-sensitive (Main is different from main).
  • Every executable Go program starts with:
    1. package main
    2. func main() { ... }